using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace ReportMaintenanceUtility
{
	public class ParameterValueList : CustomList<ParameterValue>
	{
		#region CONSTRUCTOR(S)
		public ParameterValueList() : base() { }
		public ParameterValueList(ParameterValuesType pvts)
		{
			if (pvts == null)
			{
				throw new ArgumentNullException();
			}
			else
			{
				this._initializing = true;
				this._seed = pvts;
				if (pvts.ParameterValue != null)
				{
					for (int i = 0; i < pvts.ParameterValue.Length; i++)
					{
						this.Add(new ParameterValue(pvts.ParameterValue[i]));
					}
				}
				this._initializing = false;
			}
		}
		protected ParameterValueList(ParameterValueList list) : base(list) { }
		#endregion

		#region OVERRIDES
		protected override void InsertItem(int index, ParameterValue item)
		{
			if (!this._initializing)
			{
				this.PrepStateChange("ParameterValueListItem");

				ParameterValuesType pvts = (ParameterValuesType)this._seed;
				List<ParameterValueType> pvtList = new List<ParameterValueType>(pvts.ParameterValue);

				ParameterValueType pvt = item.GetSeedObject();
				if (!pvtList.Contains(pvt))
				{
					pvtList.Add(pvt);
					pvts.ParameterValue = pvtList.ToArray();
				}
			}
			base.InsertItem(index, item);
		}
		protected override void RemoveItem(int index)
		{
			if (!this._initializing)
			{
				this.PrepStateChange("ParameterValueListItem");

				ParameterValuesType pvts = (ParameterValuesType)this._seed;
				List<ParameterValueType> pvtList = new List<ParameterValueType>(pvts.ParameterValue);

				ParameterValueType pvt = this[index].GetSeedObject();
				pvtList.Remove(pvt);

				pvts.ParameterValue = pvtList.ToArray();
			}
			base.RemoveItem(index);
		}
		protected override void SetItem(int index, ParameterValue item)
		{
			if (!this._initializing)
			{
				this.PrepStateChange("ParameterValueListItem");

				//create a temporary list of ParameterValueType objects from current seed object...
				ParameterValuesType pvts = (ParameterValuesType)this._seed;
				List<ParameterValueType> pvtList = new List<ParameterValueType>(pvts.ParameterValue);

				//find the existing ParamterValue object in the core list by index, and get its underlying seed object...
				ParameterValueType pvt = this[index].GetSeedObject();

				//if the seed object contains the ParameterValue object's seed object, replace it with the new one...
				int idx = pvtList.IndexOf(pvt);
				{
					pvtList[idx] = item.GetSeedObject();
					pvts.ParameterValue = pvtList.ToArray();
				}
			}
			base.SetItem(index, item);
		}
		protected override void ClearItems()
		{
			if (!this._initializing)
			{
				this.PrepStateChange("ParameterValueListItem");
				
				ParameterValuesType pvts = (ParameterValuesType)this._seed;
				pvts.ParameterValue = null;
			}
			base.ClearItems();
		}

		protected override void OnAddingNew(System.ComponentModel.AddingNewEventArgs e)
		{
			ParameterValuesType pvts = (ParameterValuesType)this._seed;
			List<ParameterValueType> tempList = null;

			if (pvts.ParameterValue != null && pvts.ParameterValue.Length > 0)
			{
				tempList = new List<ParameterValueType>(pvts.ParameterValue);
			}
			else
			{
				tempList = new List<ParameterValueType>();
			}
			
			ParameterValueType pvt = new ParameterValueType();
			
			List<ItemsChoiceType32> pvtElements = new List<ItemsChoiceType32>();
			pvtElements.Add(ItemsChoiceType32.Label);
			pvtElements.Add(ItemsChoiceType32.Value);
			
			List<object> pvtItems = new List<object>();
			pvtItems.Add(string.Empty);
			pvtItems.Add(string.Empty);

			pvt.ItemsElementName = pvtElements.ToArray();
			pvt.Items = pvtItems.ToArray();

			tempList.Add(pvt);
			pvts.ParameterValue = tempList.ToArray();

			ParameterValue pv = new ParameterValue(pvt);
			e.NewObject = pv;

			base.OnAddingNew(e);
		}
		public override void CancelNew(int itemIndex)
		{
			ParameterValuesType pvts = (ParameterValuesType)this._seed;
			List<ParameterValueType> tempList = new List<ParameterValueType>(pvts.ParameterValue);

			tempList.RemoveAt(itemIndex);
			pvts.ParameterValue = tempList.ToArray();

			base.CancelNew(itemIndex);
		}

		protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs e)
		{
			if (!this._initializing && e.ListChangedType == ListChangedType.ItemChanged)
			{
				this.PrepStateChange("ParameterValueListItem");
				base.OnListChanged(e);
			}
		}

		public override CustomList<ParameterValue> CreateDeepCopy()
		{
			if (this._copy != null)
			{
				return this._copy;
			}
			return new ParameterValueList(this);
		}

		#endregion
	}
}
